//
//  WEXEmoticonToolBar.swift
//  microBlog
//
//  Created by apple on 16/4/27.
//  Copyright © 2016年 Hill. All rights reserved.
//

import UIKit

enum WEXEmoticonButtonType: Int {
    
    case Recent = 0, Default, Emoji, Lxh
}



class WEXEmoticonToolBar: UIStackView {
    
    // MARK: - 供外部传入数据的属性
    var selectSection: Int = 0 {
        didSet{
            // MARK: - pay attention 这里的viewWithTag需要保证传入的tag 和本身的tag不同，不然会导致传出的类型不是需要的类型
            let button = viewWithTag(selectSection) as! UIButton
            if button != lastSelectedButton {
                
                button.selected = true
                lastSelectedButton?.selected = false
                
                lastSelectedButton = button
            }
        }
        
    }
    
    // 记录上一次选中的按钮
    var lastSelectedButton: UIButton?
    
    // MARK: - 定义闭包
    var clickEmticonButtonClosure: ((type: WEXEmoticonButtonType)->())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        
        //---------------保证tag不冲突-----------------
        tag = 20000
        //---------------保证tag不冲突-----------------
        distribution = .FillEqually
        // 添加4个按钮
        addChildButton("最近", imgName: "compose_emotion_table_left", type: .Recent)
        addChildButton("默认", imgName: "compose_emotion_table_mid", type: .Default)
        addChildButton("Emoji", imgName: "compose_emotion_table_mid", type: .Emoji)
        addChildButton("浪小花", imgName: "compose_emotion_table_right", type: .Lxh)
        
        
    }

    
    // 添加按钮的方法
    private func addChildButton(title: String, imgName: String, type: WEXEmoticonButtonType){
        
        
        let button = UIButton()
        
//        给button设置tag值
        button.tag = type.rawValue
        
//        添加按钮点击事件
        button.addTarget(self, action: "clcikButtonSelected:", forControlEvents: UIControlEvents.TouchUpInside)
//        给button设置名称
        button.setTitle(title, forState: UIControlState.Normal)
        
//        给button设置普通和选中状态下的图片
        button.setBackgroundImage(UIImage(named: "\(imgName)_normal"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "\(imgName)_selected"), forState: UIControlState.Selected)
        
//        给button设置文字大小
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
//        给button设置文字颜色
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Selected)
        
        addArrangedSubview(button)
        
        
    }
    
    @objc private func clcikButtonSelected(button: UIButton){
        
        if lastSelectedButton == button {
            
            return
        }
        
//
//        执行闭包
        clickEmticonButtonClosure?(type: WEXEmoticonButtonType(rawValue: button.tag)!)
//        取消之前选中的按钮的选中状态
        lastSelectedButton?.selected = false
//        改变当前按钮的状态
        button.selected = true
//        记录当前选中的按钮
        lastSelectedButton = button
        
    }
    
    
}
